//
// GColor.js
// hs-admin
//
// Created by 区区电脑 on 2023/05/09.
//



// FileName GColor
// Author 区区电脑
// Email 1573580882@qq.com
// Date 2021/12/18 4:30 下午

const GColorRgb = 'rgb';
const GColorRgba = 'rgba';
const GColorHex = 'Hex';

// rgb -> Hex
const rgbToHex = function (value) {
    // RGB颜色值的正则
    let reg = /^(rgb|RGB)/;
    let color = value;
    if (reg.test(color)) {
        let strHex = "#";
        // 把RGB的3个数值变成数组
        let colorArr = color.replace(/(?:\(|\)|rgb|RGB)*/g, "").split(",");
        // 转成16进制
        for (let i = 0; i < colorArr.length; i++) {
            let hex = Number(colorArr[i]).toString(16);
            if (hex === "0") {
                hex += hex;
            }
            strHex += hex;
        }
        return strHex;
    } else {
        return String(color);
    }
};

// Hex -> rgb
const hexToRgb = function (value) {
    // 16进制颜色值的正则
    let reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
    // 把颜色值变成小写
    let color = value.toLowerCase();
    if (reg.test(color)) {
        // 如果只有三位的值，需变成六位，如：#fff => #ffffff
        if (color.length === 4) {
            let colorNew = "#";
            for (let i = 1; i < 4; i += 1) {
                colorNew += color.slice(i, i + 1).concat(color.slice(i, i + 1));
            }
            color = colorNew;
        }
        // 处理六位的颜色值，转为RGB
        let colorChange = [];
        for (let i = 1; i < 7; i += 2) {
            colorChange.push(parseInt("0x" + color.slice(i, i + 2)));
        }
        return "rgb(" + colorChange.join(",") + ")";
    } else {
        return color;
    }
};
// rgb -> rgba
const rgbToRgba = function (color, alpha = 1) {
    if (alpha > 1) {
        alpha = 1
    }
    if (alpha < 0) {
        alpha = 0
    }
    color = color.replace(/rgb/, 'rgba').replace(/\)/, `, ${alpha})`)
    return color
}
// rgba -> rgb
const rgbaToRgb = function (color) {
    color = color.replace(/rgba/, 'rgb')
    const arr = color.split(',')
    arr.pop()
    return `${arr.join(',')})`
}
// rgba 半透明颜色转 Hex 和 rgb 是去掉透明度
class GColor {
    static toRgb(color) {
        const type = GColor.getType(color);
        switch (type) {
            case "Hex":
                return hexToRgb(color)
            case "rgba":
                return rgbaToRgb(color)
            case "rgb":
                return color;
            default:
                return color;
        }
    }
    static toRgba(color) {
        const type = GColor.getType(color);
        switch (type) {
            case "Hex":
                return rgbToRgba(hexToRgb(color))
            case "rgba":
                return color
            case "rgb":
                return rgbToRgba(color);
            default:
                return color;
        }
    }
    static toHex(color) {
        const type = GColor.getType(color);
        switch (type) {
            case "Hex":
                return color
            case "rgba":
                return rgbToHex(rgbaToRgb(color))
            case "rgb":
                return rgbToHex(color);
            default:
                return color;
        }
    }
    static toAlpha(color, alpha) {
        const type = GColor.getType(color);
        switch (type) {
            case "Hex":
                return rgbToRgba(hexToRgb(color), alpha)
            case "rgba":
                return rgbToRgba(rgbaToRgb(color), alpha)
            case "rgb":
                return rgbToRgba(color, alpha);
            default:
                return color;
        }
    }
    static getType(color) {
        if (color.indexOf('#') === 0) {
            return GColorHex;
        } else if (color.indexOf('rgba') === 0) {
            return GColorRgba;
        } else if (color.indexOf('rgb') === 0) {
            return GColorRgb;
        } else {
            console.error('颜色格式不正确: ', color)
        }
    }
}

export default GColor;
